home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_print.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.8 KB  |  67 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Fixer for print.
  5.  
  6. Change:
  7.     \'print\'          into \'print()\'
  8.     \'print ...\'      into \'print(...)\'
  9.     \'print ... ,\'    into \'print(..., end=" ")\'
  10.     \'print >>x, ...\' into \'print(..., file=x)\'
  11.  
  12. No changes are applied if print_function is imported from __future__
  13.  
  14. '''
  15. from  import patcomp
  16. from  import pytree
  17. from pgen2 import token
  18. from  import fixer_base
  19. from fixer_util import Name, Call, Comma, String, is_tuple
  20. parend_expr = patcomp.compile_pattern("atom< '(' [atom|STRING|NAME] ')' >")
  21.  
  22. class FixPrint(fixer_base.ConditionalFix):
  23.     PATTERN = "\n              simple_stmt< any* bare='print' any* > | print_stmt\n              "
  24.     skip_on = '__future__.print_function'
  25.     
  26.     def transform(self, node, results):
  27.         if not results:
  28.             raise AssertionError
  29.         if self.should_skip(node):
  30.             return None
  31.         bare_print = results.get('bare')
  32.         if bare_print:
  33.             bare_print.replace(Call(Name('print'), [], prefix = bare_print.get_prefix()))
  34.             return None
  35.         if not node.children[0] == Name('print'):
  36.             raise AssertionError
  37.         args = node.children[1:]
  38.         if len(args) == 1 and parend_expr.match(args[0]):
  39.             return None
  40.         sep = None
  41.         end = None
  42.         file = None
  43.         l_args = [ arg.clone() for arg in args ]
  44.         if sep is not None and end is not None or file is not None:
  45.             if end is not None:
  46.                 self.add_kwarg(l_args, 'end', String(repr(end)))
  47.             
  48.             if file is not None:
  49.                 self.add_kwarg(l_args, 'file', file)
  50.             
  51.         
  52.         n_stmt = Call(Name('print'), l_args)
  53.         n_stmt.set_prefix(node.get_prefix())
  54.         return n_stmt
  55.  
  56.     
  57.     def add_kwarg(self, l_nodes, s_kwd, n_expr):
  58.         n_expr.set_prefix('')
  59.         n_argument = pytree.Node(self.syms.argument, (Name(s_kwd), pytree.Leaf(token.EQUAL, '='), n_expr))
  60.         if l_nodes:
  61.             l_nodes.append(Comma())
  62.             n_argument.set_prefix(' ')
  63.         
  64.         l_nodes.append(n_argument)
  65.  
  66.  
  67.